﻿#region Using directives

using System;
using System.Data;
using System.Data.Common;
using System.Collections;
using System.Collections.Generic;

using ATA.Entities;
using ATA.Data;

#endregion

namespace ATA.Data.Bases
{	
	///<summary>
	/// This class is the base class for any <see cref="ChietKhauProviderBase"/> implementation.
	/// It exposes CRUD methods as well as selecting on index, foreign keys and custom stored procedures.
	///</summary>
	public abstract partial class ChietKhauProviderBaseCore : EntityProviderBase<ATA.Entities.ChietKhau, ATA.Entities.ChietKhauKey>
	{		
		#region Get from Many To Many Relationship Functions
		#endregion	
		
		#region Delete Methods

		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to delete.</param>
		/// <returns>Returns true if operation suceeded.</returns>
		public override bool Delete(TransactionManager transactionManager, ATA.Entities.ChietKhauKey key)
		{
			return Delete(transactionManager, key.ChietKhauId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="chietKhauId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public bool Delete(System.Guid chietKhauId)
		{
			return Delete(null, chietKhauId);
		}
		
		/// <summary>
		/// 	Deletes a row from the DataSource.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId">. Primary Key.</param>
		/// <remarks>Deletes based on primary key(s).</remarks>
		/// <returns>Returns true if operation suceeded.</returns>
		public abstract bool Delete(TransactionManager transactionManager, System.Guid chietKhauId);		
		
		#endregion Delete Methods
		
		#region Get By Foreign Key Functions
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		FK_ChietKhau_NhomKhachHang Description: 
		/// </summary>
		/// <param name="nhomKhachHangId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(System.Guid? nhomKhachHangId)
		{
			int count = -1;
			return GetByNhomKhachHangId(nhomKhachHangId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		FK_ChietKhau_NhomKhachHang Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomKhachHangId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(TransactionManager transactionManager, System.Guid? nhomKhachHangId)
		{
			int count = -1;
			return GetByNhomKhachHangId(transactionManager, nhomKhachHangId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		FK_ChietKhau_NhomKhachHang Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomKhachHangId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(TransactionManager transactionManager, System.Guid? nhomKhachHangId, int start, int pageLength)
		{
			int count = -1;
			return GetByNhomKhachHangId(transactionManager, nhomKhachHangId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		fkChietKhauNhomKhachHang Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomKhachHangId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(System.Guid? nhomKhachHangId, int start, int pageLength)
		{
			int count =  -1;
			return GetByNhomKhachHangId(null, nhomKhachHangId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		fkChietKhauNhomKhachHang Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="nhomKhachHangId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(System.Guid? nhomKhachHangId, int start, int pageLength,out int count)
		{
			return GetByNhomKhachHangId(null, nhomKhachHangId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_NhomKhachHang key.
		///		FK_ChietKhau_NhomKhachHang Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="nhomKhachHangId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public abstract ATA.Entities.TList<ChietKhau> GetByNhomKhachHangId(TransactionManager transactionManager, System.Guid? nhomKhachHangId, int start, int pageLength, out int count);
		
	
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		FK_ChietKhau_DieuKienGiamGia Description: 
		/// </summary>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(System.Guid? dieuKienGiamGiaId)
		{
			int count = -1;
			return GetByDieuKienGiamGiaId(dieuKienGiamGiaId, 0,int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		FK_ChietKhau_DieuKienGiamGia Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		/// <remarks></remarks>
		public ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(TransactionManager transactionManager, System.Guid? dieuKienGiamGiaId)
		{
			int count = -1;
			return GetByDieuKienGiamGiaId(transactionManager, dieuKienGiamGiaId, 0, int.MaxValue, out count);
		}
		
			/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		FK_ChietKhau_DieuKienGiamGia Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		///  <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(TransactionManager transactionManager, System.Guid? dieuKienGiamGiaId, int start, int pageLength)
		{
			int count = -1;
			return GetByDieuKienGiamGiaId(transactionManager, dieuKienGiamGiaId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		fkChietKhauDieuKienGiamGia Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(System.Guid? dieuKienGiamGiaId, int start, int pageLength)
		{
			int count =  -1;
			return GetByDieuKienGiamGiaId(null, dieuKienGiamGiaId, start, pageLength,out count);	
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		fkChietKhauDieuKienGiamGia Description: 
		/// </summary>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(System.Guid? dieuKienGiamGiaId, int start, int pageLength,out int count)
		{
			return GetByDieuKienGiamGiaId(null, dieuKienGiamGiaId, start, pageLength, out count);	
		}
						
		/// <summary>
		/// 	Gets rows from the datasource based on the FK_ChietKhau_DieuKienGiamGia key.
		///		FK_ChietKhau_DieuKienGiamGia Description: 
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="dieuKienGiamGiaId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns a typed collection of ATA.Entities.ChietKhau objects.</returns>
		public abstract ATA.Entities.TList<ChietKhau> GetByDieuKienGiamGiaId(TransactionManager transactionManager, System.Guid? dieuKienGiamGiaId, int start, int pageLength, out int count);
		
		#endregion

		#region Get By Index Functions
		
		/// <summary>
		/// 	Gets a row from the DataSource based on its primary key.
		/// </summary>
		/// <param name="transactionManager">A <see cref="TransactionManager"/> object.</param>
		/// <param name="key">The unique identifier of the row to retrieve.</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <returns>Returns an instance of the Entity class.</returns>
		public override ATA.Entities.ChietKhau Get(TransactionManager transactionManager, ATA.Entities.ChietKhauKey key, int start, int pageLength)
		{
			return GetByChietKhauId(transactionManager, key.ChietKhauId, start, pageLength);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the primary key PK_ChietKhau index.
		/// </summary>
		/// <param name="chietKhauId"></param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public ATA.Entities.ChietKhau GetByChietKhauId(System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(null,chietKhauId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhau index.
		/// </summary>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public ATA.Entities.ChietKhau GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauId(null, chietKhauId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhau index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public ATA.Entities.ChietKhau GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, 0, int.MaxValue, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhau index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public ATA.Entities.ChietKhau GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength)
		{
			int count = -1;
			return GetByChietKhauId(transactionManager, chietKhauId, start, pageLength, out count);
		}
		
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhau index.
		/// </summary>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">out parameter to get total records for query</param>
		/// <remarks></remarks>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public ATA.Entities.ChietKhau GetByChietKhauId(System.Guid chietKhauId, int start, int pageLength, out int count)
		{
			return GetByChietKhauId(null, chietKhauId, start, pageLength, out count);
		}
		
				
		/// <summary>
		/// 	Gets rows from the datasource based on the PK_ChietKhau index.
		/// </summary>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="chietKhauId"></param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">Number of rows to return.</param>
		/// <param name="count">The total number of records.</param>
		/// <returns>Returns an instance of the <see cref="ATA.Entities.ChietKhau"/> class.</returns>
		public abstract ATA.Entities.ChietKhau GetByChietKhauId(TransactionManager transactionManager, System.Guid chietKhauId, int start, int pageLength, out int count);
						
		#endregion "Get By Index Functions"
	
		#region Custom Methods
		
		
		#endregion

		#region Helper Functions	
		
		/// <summary>
		/// Fill a ATA.Entities.TList&lt;ChietKhau&gt; From a DataReader.
		/// </summary>
		/// <param name="reader">Datareader</param>
		/// <param name="rows">The collection to fill</param>
		/// <param name="start">Row number at which to start reading, the first row is 0.</param>
		/// <param name="pageLength">number of rows.</param>
		/// <returns>a <see cref="ATA.Entities.TList&lt;ChietKhau&gt;"/></returns>
		public static ATA.Entities.TList<ChietKhau> Fill(IDataReader reader, ATA.Entities.TList<ChietKhau> rows, int start, int pageLength)
		{
		// advance to the starting row
		for (int i = 0; i < start; i++)
		{
			if (!reader.Read())
			return rows; // not enough rows, just return
		}
		for (int i = 0; i < pageLength; i++)
		{
			if (!reader.Read())
			break; // we are done
			string key = null;
			
			ATA.Entities.ChietKhau c = null;
			if (DataRepository.Provider.UseEntityFactory)
			{
			key = new System.Text.StringBuilder("ChietKhau")
			.Append("|").Append((reader.IsDBNull(((int)ATA.Entities.ChietKhauColumn.ChietKhauId - 1))?Guid.Empty:(System.Guid)reader[((int)ATA.Entities.ChietKhauColumn.ChietKhauId - 1)]).ToString()).ToString();
			c = EntityManager.LocateOrCreate<ChietKhau>(
			key.ToString(), // EntityTrackingKey
			"ChietKhau",  //Creational Type
			DataRepository.Provider.EntityCreationalFactoryType,  //Factory used to create entity
			DataRepository.Provider.EnableEntityTracking); // Track this entity?
			}
			else
			{
			c = new ATA.Entities.ChietKhau();
			}
			
			if (!DataRepository.Provider.EnableEntityTracking ||
			c.EntityState == EntityState.Added ||
			(DataRepository.Provider.EnableEntityTracking &&
			((DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.PreserveChanges && c.EntityState == EntityState.Unchanged) ||
			(DataRepository.Provider.CurrentLoadPolicy == LoadPolicy.DiscardChanges && (c.EntityState == EntityState.Unchanged ||
								c.EntityState == EntityState.Changed)))))
						{
			c.SuppressEntityEvents = true;
			c.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			c.OriginalChietKhauId = c.ChietKhauId;
			c.StartDate = (System.DateTime)reader["StartDate"];
			c.EndDate = reader.IsDBNull(reader.GetOrdinal("EndDate")) ? null : (System.DateTime?)reader["EndDate"];
			c.DieuKienGiamGiaId = reader.IsDBNull(reader.GetOrdinal("DieuKienGiamGiaID")) ? null : (System.Guid?)reader["DieuKienGiamGiaID"];
			c.GhiChu = reader.IsDBNull(reader.GetOrdinal("GhiChu")) ? null : (System.String)reader["GhiChu"];
			c.LastUpdate = (System.DateTime)reader["LastUpdate"];
			c.UpdateBy = (System.Int32)reader["UpdateBy"];
			c.Header = (System.String)reader["Header"];
			c.NhomKhachHangId = reader.IsDBNull(reader.GetOrdinal("NhomKhachHangID")) ? null : (System.Guid?)reader["NhomKhachHangID"];
			c.EntityTrackingKey = key;
			c.AcceptChanges();
			c.SuppressEntityEvents = false;
			}
			rows.Add(c);
		}
		return rows;
		}		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhau"/> object from the <see cref="IDataReader"/>.
		/// </summary>
		/// <param name="reader">The <see cref="IDataReader"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhau"/> object to refresh.</param>
		public static void RefreshEntity(IDataReader reader, ATA.Entities.ChietKhau entity)
		{
			if (!reader.Read()) return;
			
			entity.ChietKhauId = (System.Guid)reader["ChietKhauID"];
			entity.OriginalChietKhauId = (System.Guid)reader["ChietKhauID"];
			entity.StartDate = (System.DateTime)reader["StartDate"];
			entity.EndDate = reader.IsDBNull(reader.GetOrdinal("EndDate")) ? null : (System.DateTime?)reader["EndDate"];
			entity.DieuKienGiamGiaId = reader.IsDBNull(reader.GetOrdinal("DieuKienGiamGiaID")) ? null : (System.Guid?)reader["DieuKienGiamGiaID"];
			entity.GhiChu = reader.IsDBNull(reader.GetOrdinal("GhiChu")) ? null : (System.String)reader["GhiChu"];
			entity.LastUpdate = (System.DateTime)reader["LastUpdate"];
			entity.UpdateBy = (System.Int32)reader["UpdateBy"];
			entity.Header = (System.String)reader["Header"];
			entity.NhomKhachHangId = reader.IsDBNull(reader.GetOrdinal("NhomKhachHangID")) ? null : (System.Guid?)reader["NhomKhachHangID"];
			entity.AcceptChanges();
		}
		
		/// <summary>
		/// Refreshes the <see cref="ATA.Entities.ChietKhau"/> object from the <see cref="DataSet"/>.
		/// </summary>
		/// <param name="dataSet">The <see cref="DataSet"/> to read from.</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhau"/> object.</param>
		public static void RefreshEntity(DataSet dataSet, ATA.Entities.ChietKhau entity)
		{
			DataRow dataRow = dataSet.Tables[0].Rows[0];
			
			entity.ChietKhauId = (System.Guid)dataRow["ChietKhauID"];
			entity.OriginalChietKhauId = (System.Guid)dataRow["ChietKhauID"];
			entity.StartDate = (System.DateTime)dataRow["StartDate"];
			entity.EndDate = Convert.IsDBNull(dataRow["EndDate"]) ? null : (System.DateTime?)dataRow["EndDate"];
			entity.DieuKienGiamGiaId = Convert.IsDBNull(dataRow["DieuKienGiamGiaID"]) ? null : (System.Guid?)dataRow["DieuKienGiamGiaID"];
			entity.GhiChu = Convert.IsDBNull(dataRow["GhiChu"]) ? null : (System.String)dataRow["GhiChu"];
			entity.LastUpdate = (System.DateTime)dataRow["LastUpdate"];
			entity.UpdateBy = (System.Int32)dataRow["UpdateBy"];
			entity.Header = (System.String)dataRow["Header"];
			entity.NhomKhachHangId = Convert.IsDBNull(dataRow["NhomKhachHangID"]) ? null : (System.Guid?)dataRow["NhomKhachHangID"];
			entity.AcceptChanges();
		}
		#endregion 
		
		#region DeepLoad Methods
		/// <summary>
		/// Deep Loads the <see cref="IEntity"/> object with criteria based of the child 
		/// property collections only N Levels Deep based on the <see cref="DeepLoadType"/>.
		/// </summary>
		/// <remarks>
		/// Use this method with caution as it is possible to DeepLoad with Recursion and traverse an entire object graph.
		/// </remarks>
		/// <param name="transactionManager"><see cref="TransactionManager"/> object</param>
		/// <param name="entity">The <see cref="ATA.Entities.ChietKhau"/> object to load.</param>
		/// <param name="deep">Boolean. A flag that indicates whether to recursively save all Property Collection that are descendants of this instance. If True, saves the complete object graph below this object. If False, saves this object only. </param>
		/// <param name="deepLoadType">DeepLoadType Enumeration to Include/Exclude object property collections from Load.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhau Property Collection Type Array To Include or Exclude from Load</param>
		/// <param name="innerList">A collection of child types for easy access.</param>
	    /// <exception cref="ArgumentNullException">entity or childTypes is null.</exception>
	    /// <exception cref="ArgumentException">deepLoadType has invalid value.</exception>
		internal override void DeepLoad(TransactionManager transactionManager, ATA.Entities.ChietKhau entity, bool deep, DeepLoadType deepLoadType, System.Type[] childTypes, DeepSession innerList)
		{
			if(entity == null)
				return;

			#region NhomKhachHangIdSource	
			if (CanDeepLoad(entity, "NhomKhachHang|NhomKhachHangIdSource", deepLoadType, innerList) 
				&& entity.NhomKhachHangIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.NhomKhachHangId ?? Guid.Empty);
				NhomKhachHang tmpEntity = EntityManager.LocateEntity<NhomKhachHang>(EntityLocator.ConstructKeyFromPkItems(typeof(NhomKhachHang), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.NhomKhachHangIdSource = tmpEntity;
				else
					entity.NhomKhachHangIdSource = DataRepository.NhomKhachHangProvider.GetByNhomKhachHangId(transactionManager, (entity.NhomKhachHangId ?? Guid.Empty));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'NhomKhachHangIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.NhomKhachHangIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.NhomKhachHangProvider.DeepLoad(transactionManager, entity.NhomKhachHangIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion NhomKhachHangIdSource

			#region DieuKienGiamGiaIdSource	
			if (CanDeepLoad(entity, "DieuKienGiamGia|DieuKienGiamGiaIdSource", deepLoadType, innerList) 
				&& entity.DieuKienGiamGiaIdSource == null)
			{
				object[] pkItems = new object[1];
				pkItems[0] = (entity.DieuKienGiamGiaId ?? Guid.Empty);
				DieuKienGiamGia tmpEntity = EntityManager.LocateEntity<DieuKienGiamGia>(EntityLocator.ConstructKeyFromPkItems(typeof(DieuKienGiamGia), pkItems), DataRepository.Provider.EnableEntityTracking);
				if (tmpEntity != null)
					entity.DieuKienGiamGiaIdSource = tmpEntity;
				else
					entity.DieuKienGiamGiaIdSource = DataRepository.DieuKienGiamGiaProvider.GetByDieuKienId(transactionManager, (entity.DieuKienGiamGiaId ?? Guid.Empty));		
				
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'DieuKienGiamGiaIdSource' loaded. key " + entity.EntityTrackingKey);
				#endif 
				
				if (deep && entity.DieuKienGiamGiaIdSource != null)
				{
					innerList.SkipChildren = true;
					DataRepository.DieuKienGiamGiaProvider.DeepLoad(transactionManager, entity.DieuKienGiamGiaIdSource, deep, deepLoadType, childTypes, innerList);
					innerList.SkipChildren = false;
				}
					
			}
			#endregion DieuKienGiamGiaIdSource
			
			//used to hold DeepLoad method delegates and fire after all the local children have been loaded.
			Dictionary<string, KeyValuePair<Delegate, object>> deepHandles = new Dictionary<string, KeyValuePair<Delegate, object>>();
			// Deep load child collections  - Call GetByChietKhauId methods when available
			
			#region ChietKhauDoanhSoCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ChietKhauDoanhSo>|ChietKhauDoanhSoCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauDoanhSoCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ChietKhauDoanhSoCollection = DataRepository.ChietKhauDoanhSoProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);

				if (deep && entity.ChietKhauDoanhSoCollection.Count > 0)
				{
					deepHandles.Add("ChietKhauDoanhSoCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ChietKhauDoanhSo>) DataRepository.ChietKhauDoanhSoProvider.DeepLoad,
						new object[] { transactionManager, entity.ChietKhauDoanhSoCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ChietKhauNhanhCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ChietKhauNhanh>|ChietKhauNhanhCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauNhanhCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ChietKhauNhanhCollection = DataRepository.ChietKhauNhanhProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);

				if (deep && entity.ChietKhauNhanhCollection.Count > 0)
				{
					deepHandles.Add("ChietKhauNhanhCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ChietKhauNhanh>) DataRepository.ChietKhauNhanhProvider.DeepLoad,
						new object[] { transactionManager, entity.ChietKhauNhanhCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			#region ChietKhauSanPhamCollection
			//Relationship Type One : Many
			if (CanDeepLoad(entity, "List<ChietKhauSanPham>|ChietKhauSanPhamCollection", deepLoadType, innerList)) 
			{
				#if NETTIERS_DEBUG
				System.Diagnostics.Debug.WriteLine("- property 'ChietKhauSanPhamCollection' loaded. key " + entity.EntityTrackingKey);
				#endif 

				entity.ChietKhauSanPhamCollection = DataRepository.ChietKhauSanPhamProvider.GetByChietKhauId(transactionManager, entity.ChietKhauId);

				if (deep && entity.ChietKhauSanPhamCollection.Count > 0)
				{
					deepHandles.Add("ChietKhauSanPhamCollection",
						new KeyValuePair<Delegate, object>((DeepLoadHandle<ChietKhauSanPham>) DataRepository.ChietKhauSanPhamProvider.DeepLoad,
						new object[] { transactionManager, entity.ChietKhauSanPhamCollection, deep, deepLoadType, childTypes, innerList }
					));
				}
			}		
			#endregion 
			
			
			//Fire all DeepLoad Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles.Values)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			deepHandles = null;
		}
		
		#endregion 
		
		#region DeepSave Methods

		/// <summary>
		/// Deep Save the entire object graph of the ATA.Entities.ChietKhau object with criteria based of the child 
		/// Type property array and DeepSaveType.
		/// </summary>
		/// <param name="transactionManager">The transaction manager.</param>
		/// <param name="entity">ATA.Entities.ChietKhau instance</param>
		/// <param name="deepSaveType">DeepSaveType Enumeration to Include/Exclude object property collections from Save.</param>
		/// <param name="childTypes">ATA.Entities.ChietKhau Property Collection Type Array To Include or Exclude from Save</param>
		/// <param name="innerList">A Hashtable of child types for easy access.</param>
		internal override bool DeepSave(TransactionManager transactionManager, ATA.Entities.ChietKhau entity, DeepSaveType deepSaveType, System.Type[] childTypes, DeepSession innerList)
		{	
			if (entity == null)
				return false;
							
			#region Composite Parent Properties
			//Save Source Composite Properties, however, don't call deep save on them.  
			//So they only get saved a single level deep.
			
			#region NhomKhachHangIdSource
			if (CanDeepSave(entity, "NhomKhachHang|NhomKhachHangIdSource", deepSaveType, innerList) 
				&& entity.NhomKhachHangIdSource != null)
			{
				DataRepository.NhomKhachHangProvider.Save(transactionManager, entity.NhomKhachHangIdSource);
				entity.NhomKhachHangId = entity.NhomKhachHangIdSource.NhomKhachHangId;
			}
			#endregion 
			
			#region DieuKienGiamGiaIdSource
			if (CanDeepSave(entity, "DieuKienGiamGia|DieuKienGiamGiaIdSource", deepSaveType, innerList) 
				&& entity.DieuKienGiamGiaIdSource != null)
			{
				DataRepository.DieuKienGiamGiaProvider.Save(transactionManager, entity.DieuKienGiamGiaIdSource);
				entity.DieuKienGiamGiaId = entity.DieuKienGiamGiaIdSource.DieuKienId;
			}
			#endregion 
			#endregion Composite Parent Properties

			// Save Root Entity through Provider
			if (!entity.IsDeleted)
				this.Save(transactionManager, entity);
			
			//used to hold DeepSave method delegates and fire after all the local children have been saved.
			Dictionary<Delegate, object> deepHandles = new Dictionary<Delegate, object>();
	
			#region List<ChietKhauDoanhSo>
				if (CanDeepSave(entity.ChietKhauDoanhSoCollection, "List<ChietKhauDoanhSo>|ChietKhauDoanhSoCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ChietKhauDoanhSo child in entity.ChietKhauDoanhSoCollection)
					{
						if(child.ChietKhauIdSource != null)
							child.ChietKhauId = child.ChietKhauIdSource.ChietKhauId;
						else
							child.ChietKhauId = entity.ChietKhauId;

					}

					if (entity.ChietKhauDoanhSoCollection.Count > 0 || entity.ChietKhauDoanhSoCollection.DeletedItems.Count > 0)
					{
						DataRepository.ChietKhauDoanhSoProvider.Save(transactionManager, entity.ChietKhauDoanhSoCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< ChietKhauDoanhSo >) DataRepository.ChietKhauDoanhSoProvider.DeepSave,
							new object[] { transactionManager, entity.ChietKhauDoanhSoCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
	
			#region List<ChietKhauNhanh>
				if (CanDeepSave(entity.ChietKhauNhanhCollection, "List<ChietKhauNhanh>|ChietKhauNhanhCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ChietKhauNhanh child in entity.ChietKhauNhanhCollection)
					{
						if(child.ChietKhauIdSource != null)
							child.ChietKhauId = child.ChietKhauIdSource.ChietKhauId;
						else
							child.ChietKhauId = entity.ChietKhauId;

					}

					if (entity.ChietKhauNhanhCollection.Count > 0 || entity.ChietKhauNhanhCollection.DeletedItems.Count > 0)
					{
						DataRepository.ChietKhauNhanhProvider.Save(transactionManager, entity.ChietKhauNhanhCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< ChietKhauNhanh >) DataRepository.ChietKhauNhanhProvider.DeepSave,
							new object[] { transactionManager, entity.ChietKhauNhanhCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
	
			#region List<ChietKhauSanPham>
				if (CanDeepSave(entity.ChietKhauSanPhamCollection, "List<ChietKhauSanPham>|ChietKhauSanPhamCollection", deepSaveType, innerList)) 
				{	
					// update each child parent id with the real parent id (mostly used on insert)
					foreach(ChietKhauSanPham child in entity.ChietKhauSanPhamCollection)
					{
						if(child.ChietKhauIdSource != null)
							child.ChietKhauId = child.ChietKhauIdSource.ChietKhauId;
						else
							child.ChietKhauId = entity.ChietKhauId;

					}

					if (entity.ChietKhauSanPhamCollection.Count > 0 || entity.ChietKhauSanPhamCollection.DeletedItems.Count > 0)
					{
						DataRepository.ChietKhauSanPhamProvider.Save(transactionManager, entity.ChietKhauSanPhamCollection);
						
						deepHandles.Add(
							(DeepSaveHandle< ChietKhauSanPham >) DataRepository.ChietKhauSanPhamProvider.DeepSave,
							new object[] { transactionManager, entity.ChietKhauSanPhamCollection, deepSaveType, childTypes, innerList }
						);
					}
				} 
			#endregion 
				
			//Fire all DeepSave Items
			foreach(KeyValuePair<Delegate, object> pair in deepHandles)
		    {
                pair.Key.DynamicInvoke((object[])pair.Value);
		    }
			
			// Save Root Entity through Provider, if not already saved in delete mode
			if (entity.IsDeleted)
				this.Save(transactionManager, entity);
				

			deepHandles = null;
						
			return true;
		}
		#endregion
	} // end class
	
	#region ChietKhauChildEntityTypes
	
	///<summary>
	/// Enumeration used to expose the different child entity types 
	/// for child properties in <c>ATA.Entities.ChietKhau</c>
	///</summary>
	public enum ChietKhauChildEntityTypes
	{
		
		///<summary>
		/// Composite Property for <c>NhomKhachHang</c> at NhomKhachHangIdSource
		///</summary>
		[ChildEntityType(typeof(NhomKhachHang))]
		NhomKhachHang,
			
		///<summary>
		/// Composite Property for <c>DieuKienGiamGia</c> at DieuKienGiamGiaIdSource
		///</summary>
		[ChildEntityType(typeof(DieuKienGiamGia))]
		DieuKienGiamGia,
	
		///<summary>
		/// Collection of <c>ChietKhau</c> as OneToMany for ChietKhauDoanhSoCollection
		///</summary>
		[ChildEntityType(typeof(TList<ChietKhauDoanhSo>))]
		ChietKhauDoanhSoCollection,

		///<summary>
		/// Collection of <c>ChietKhau</c> as OneToMany for ChietKhauNhanhCollection
		///</summary>
		[ChildEntityType(typeof(TList<ChietKhauNhanh>))]
		ChietKhauNhanhCollection,

		///<summary>
		/// Collection of <c>ChietKhau</c> as OneToMany for ChietKhauSanPhamCollection
		///</summary>
		[ChildEntityType(typeof(TList<ChietKhauSanPham>))]
		ChietKhauSanPhamCollection,
	}
	
	#endregion ChietKhauChildEntityTypes
	
	#region ChietKhauFilterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilterBuilder&lt;ChietKhauColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhau"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauFilterBuilder : SqlFilterBuilder<ChietKhauColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauFilterBuilder class.
		/// </summary>
		public ChietKhauFilterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauFilterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauFilterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauFilterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauFilterBuilder
	
	#region ChietKhauParameterBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ParameterizedSqlFilterBuilder&lt;ChietKhauColumn&gt;"/> class
	/// that is used exclusively with a <see cref="ChietKhau"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class ChietKhauParameterBuilder : ParameterizedSqlFilterBuilder<ChietKhauColumn>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the ChietKhauParameterBuilder class.
		/// </summary>
		public ChietKhauParameterBuilder() : base() { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		public ChietKhauParameterBuilder(bool ignoreCase) : base(ignoreCase) { }

		/// <summary>
		/// Initializes a new instance of the ChietKhauParameterBuilder class.
		/// </summary>
		/// <param name="ignoreCase">Specifies whether to create case-insensitive statements.</param>
		/// <param name="useAnd">Specifies whether to combine statements using AND or OR.</param>
		public ChietKhauParameterBuilder(bool ignoreCase, bool useAnd) : base(ignoreCase, useAnd) { }

		#endregion Constructors
	}

	#endregion ChietKhauParameterBuilder
} // end namespace
